<%--
  Created by IntelliJ IDEA.
  User: cjq
  Date: 2020/2/6
  Time: 22:47
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <meta charset="utf-8">
    <title>配菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="${ctxLayui}/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctxLayui}/layuiadmin/style/admin.css" media="all">
    <link rel="stylesheet" href="${ctxLayui}/layuiadmin/style/myStyle.css" media="all">
</head>
<body>
<div class="layui-form" lay-filter="layuiadmin-app-form-list" id="layuiadmin-app-form-list" style="padding: 20px 10px 0 0;">

    <div  class="layui-form-item layui-form-item-90" id="tipsDiv" style="display:none;">
        <label class="layui-form-label" style="text-align:left;width:100%;color:red;">该角色拥有全部权限</label>
    </div>

    <div  class="layui-form-item layui-form-item-90" >
        <div id="menuTree"></div>
    </div>


    <div class="layui-form-item layui-hide">
        <input type="button" lay-submit lay-filter="layuiadmin-app-form-submit" id="layuiadmin-app-form-submit" value="保存">
    </div>
</div>


<script src="${ctxLayui}/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '${ctxLayui}/layuiadmin/'
    }).extend({
        index: 'lib/index'
    }).use(['index', 'form','tree'], function(){
        var $ = layui.$ ,form = layui.form;
        var tree = layui.tree;
        var admin = layui.admin;

        var formData = window.parent.formData;
        var id = formData.id;

        var hasAllRight = formData.hasAllRight;
        if(hasAllRight==1){
            $("#tipsDiv").show();
        }

        var url = ctx+'/system/sysMenu/sysMenuTreeByRole';
        var rtn = admin.syncReq(url,{roleId:id});
        var rtnData = rtn.data;

        var inst1 = tree.render({
            elem: '#menuTree'  //绑定元素
            ,showCheckbox:true
            ,data:rtnData
            ,id:'menuTreeId'
            ,oncheck:function(obj){
            }
        });


        /*var roleMenus = admin.syncReq(ctx+'//system/sysRoleMenu/queryByAll',{roleId:id});
        var checkData = roleMenus.data;
        if(checkData && checkData.length>0){
            var checkIdArr = [];
            for(var i=0;i<checkData.length;i++){
                checkIdArr.push(checkData[i].menuId);
            }
            console.log("checkIdArr...",checkIdArr)
            tree.setChecked('menuTreeId',checkIdArr);
        }*/


        layui.getFormField=function(){
            var nodeIdArr = active.treeCheckNodes("menuTreeId");
            var field = {};
            if(nodeIdArr && nodeIdArr.length>0){
                field = {
                    roleId:id,
                    menuIds:nodeIdArr.join(",")
                }
            }else{
                field = {
                    roleId:id
                }
            }
            return field;
        }

        var active = {
            treeCheckNodes:function(treeId){
                var nodeIdArr = [];
                var nodeArr = tree.getChecked(treeId);
                for(var i=0;i<nodeArr.length;i++){
                    active.childrenCheckNodes(nodeArr[i],nodeIdArr);
                }
                return nodeIdArr;
            },
            childrenCheckNodes:function(node,nodeIdArr){
                var children = node.children;
                if(children && children.length>0){
                    for(var i=0;i<children.length;i++){
                        active.childrenCheckNodes(children[i],nodeIdArr);
                    }
                }
                nodeIdArr.push(node.id)
            }
        }

    })
</script>
</body>
</html>
